package com.ase.mobile.Listener;

import com.ase.mobile.entity.User;
import com.ase.mobile.mapper.UserMapper;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author gyhstart
 * @create 2021/5/25 - 2:15
 **/
@Component
public class UserReceiveListener {
    private static Logger log = LoggerFactory.getLogger(UserReceiveListener.class);

    @Autowired
    private UserMapper userMapper;

    @RabbitListener(queues = "queue_user")
    public void receiveMessage(User user, Channel channel, Message message) {
        // 只包含发送的消息
        log.info("接收到消息{}", user);
        int iUser = userMapper.insertSelective(user);
        if (iUser == 1) {
            log.info("添加{}用户成功", user.getName());
        }else {
            log.error("添加{}用户失败", user.getName());
        }
    }
}
